<template>
  <div class="order-detail-container">
    <div class="order-progress">
      <div class="progress-step" v-for="(step, idx) in steps" :key="step.name">
        <div :class="['step-dot', { active: idx <= currentStep }]" />
        <div class="step-label">{{ step.label }}</div>
        <div class="step-time">{{ step.time }}</div>
      </div>
    </div>
    <div class="order-main">
      <div class="order-info">
        <div class="order-header">
          <span>订单号：{{ orderDetail.order_no }}</span>
          <span>下单时间：{{ orderDetail.created_at }}</span>
          <span>订单类型：{{ orderDetail.order_type }}</span>
          <span>支付方式：{{ orderDetail.pay_type }}</span>
          <el-link type="primary" @click="showOrderLog">订单日志</el-link>
        </div>
        <el-table :data="orderDetail.goods" border stripe>
          <el-table-column label="商品信息1111">
            <template #default="scope">
              <div class="goods-info">
                <img :src="scope.row.image" class="goods-img" />
                <div>{{ scope.row.name }}</div>
              </div>
            </template>
          </el-table-column>
          <el-table-column prop="num" label="单价/数量" />
          <el-table-column prop="status" label="状态" />
          <el-table-column prop="total_price" label="订单金额" />
          <el-table-column prop="discount" label="优惠信息" />
          <el-table-column prop="merchant_price" label="商家改价" />
          <el-table-column prop="user_pay" label="用户应付" />
          <el-table-column prop="merchant_income" label="商家应收" />
        </el-table>
        <div class="order-remark">
          <span>备注信息</span>
          <el-link type="primary" @click="editRemark">修改备注</el-link>
        </div>
        <div class="order-remark-content">{{ orderDetail.remark || '暂无备注信息' }}</div>
      </div>
      <div class="order-address">
        <div class="address-header">
          <span>收货信息</span>
          <el-link type="primary" @click="editAddress">修改地址</el-link>
        </div>
        <div class="address-content">
          <div>收货人：{{ orderDetail.receiver }} 联系方式：{{ orderDetail.phone }}</div>
          <div>收货地址：{{ orderDetail.address }}</div>
        </div>
      </div>
      <div class="order-logistics">
        <span>物流详情</span>
        <div v-if="orderDetail.logistics && orderDetail.logistics.length">
          <div v-for="item in orderDetail.logistics" :key="item.id">{{ item.info }}</div>
        </div>
        <div v-else>暂无物流信息</div>
      </div>
    </div>
    <el-dialog v-model="remarkDialogVisible" title="修改备注">
      <el-input v-model="remarkInput" type="textarea" rows="3" />
      <template #footer>
        <el-button @click="remarkDialogVisible = false">取消</el-button>
        <el-button type="primary" @click="saveRemark">保存</el-button>
      </template>
    </el-dialog>
    <el-dialog v-model="addressDialogVisible" title="修改地址">
      <el-form :model="addressForm">
        <el-form-item label="收货人">
          <el-input v-model="addressForm.receiver" />
        </el-form-item>
        <el-form-item label="联系方式">
          <el-input v-model="addressForm.phone" />
        </el-form-item>
        <el-form-item label="收货地址">
          <el-input v-model="addressForm.address" />
        </el-form-item>
      </el-form>
      <template #footer>
        <el-button @click="addressDialogVisible = false">取消</el-button>
        <el-button type="primary" @click="saveAddress">保存</el-button>
      </template>
    </el-dialog>
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import { useRoute } from 'vue-router'
import { ElMessage } from 'element-plus'

const route = useRoute()
const orderDetail = ref({})
const steps = ref([
  { name: 'design', label: '用户设计', time: '' },
  { name: 'order', label: '用户下单', time: '' },
  { name: 'deep', label: '深化设计', time: '' },
  { name: 'make', label: '手办制作', time: '' },
  { name: 'send', label: '商品发货', time: '' },
  { name: 'finish', label: '交易完成', time: '' }
])
const currentStep = ref(0)
const remarkDialogVisible = ref(false)
const remarkInput = ref('')
const addressDialogVisible = ref(false)
const addressForm = ref({ receiver: '', phone: '', address: '' })

function fetchOrderDetail() {
  debugger;// TODO: 调用接口获取订单详情
  // 示例数据
  orderDetail.value = {
    order_no: 'P768699179311430201',
    created_at: '2025-07-11 18:52:01',
    order_type: '全款预付订单',
    pay_type: '支付宝免密支付',
    goods: [
      {
        image: 'https://img.example.com/goods1.jpg',
        name: '定制个人手办手卡立式枫超同款桌面',
        num: 'x2',
        status: '待发货',
        total_price: '￥59.80',
        discount: '店铺优惠 ￥50.00',
        merchant_price: '￥0.00',
        user_pay: '￥9.80',
        merchant_income: '￥9.80'
      }
    ],
    remark: '',
    receiver: '咸鱼思恩',
    phone: '157****6408',
    address: '浙江省 杭州市 滨江区 西兴街道 ***',
    logistics: []
  }
  steps.value[0].time = '2025-7-11 18:52:01'
  steps.value[1].time = '2025-7-11 19:01:22'
  steps.value[2].time = '2025-7-11 18:52:01'
  steps.value[3].time = '2025-7-11 18:52:01'
  steps.value[4].time = '2025-7-11 18:52:01'
  steps.value[5].time = '2025-7-11 18:52:01'
  currentStep.value = 3
}

function showOrderLog() {
  ElMessage.info('订单日志功能开发中')
}
function editRemark() {
  remarkInput.value = orderDetail.value.remark
  remarkDialogVisible.value = true
}
function saveRemark() {
  orderDetail.value.remark = remarkInput.value
  remarkDialogVisible.value = false
  ElMessage.success('备注已保存')
}
function editAddress() {
  addressForm.value = {
    receiver: orderDetail.value.receiver,
    phone: orderDetail.value.phone,
    address: orderDetail.value.address
  }
  addressDialogVisible.value = true
}
function saveAddress() {
  orderDetail.value.receiver = addressForm.value.receiver
  orderDetail.value.phone = addressForm.value.phone
  orderDetail.value.address = addressForm.value.address
  addressDialogVisible.value = false
  ElMessage.success('地址已保存')
}

onMounted(() => {
  fetchOrderDetail()
})
</script>

<style scoped>
.order-detail-container {
  background: #fff;
  padding: 24px;
}
.order-progress {
  display: flex;
  justify-content: space-between;
  margin-bottom: 32px;
  border-bottom: 1px solid #f0f0f0;
  padding-bottom: 24px;
}
.progress-step {
  flex: 1;
  text-align: center;
}
.step-dot {
  width: 12px;
  height: 12px;
  border-radius: 50%;
  background: #d9d9d9;
  margin: 0 auto 8px;
}
.step-dot.active {
  background: #409eff;
}
.step-label {
  font-size: 14px;
  color: #333;
}
.step-time {
  font-size: 12px;
  color: #999;
  margin-top: 4px;
}
.order-main {
  margin-top: 24px;
}
.order-info {
  margin-bottom: 24px;
}
.order-header {
  display: flex;
  gap: 24px;
  align-items: center;
  margin-bottom: 16px;
}
.goods-info {
  display: flex;
  align-items: center;
}
.goods-img {
  width: 40px;
  height: 40px;
  margin-right: 8px;
  border-radius: 4px;
  object-fit: cover;
}
.order-remark {
  display: flex;
  align-items: center;
  margin-top: 16px;
  gap: 8px;
}
.order-remark-content {
  color: #666;
  margin-top: 8px;
}
.order-address {
  margin-bottom: 24px;
}
.address-header {
  display: flex;
  align-items: center;
  gap: 8px;
  margin-bottom: 8px;
}
.address-content {
  color: #666;
}
.order-logistics {
  margin-bottom: 24px;
}
</style>